<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SocialEnergyContent extends Model
{
    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'content_type', 'title', 'content', 'target_ability', 'trigger_condition', 'is_active'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'is_active' => 'boolean',
        'created_at' => 'datetime',
    ];

    /**
     * JSON字段的访问器和修改器
     */
    public function getTriggerConditionAttribute($value)
    {
        return $value ? json_decode($value, true) : null;
    }

    public function setTriggerConditionAttribute($value)
    {
        $this->attributes['trigger_condition'] = $value ? json_encode($value) : null;
    }

    /**
     * 获取内容类型的中文名称
     */
    public function getTypeNameAttribute()
    {
        $types = [
            'encouragement' => '鼓励',
            'suggestion' => '建议',
            'tip' => '小贴士'
        ];

        return $types[$this->content_type] ?? '未知类型';
    }

    /**
     * 获取目标能力的中文名称
     */
    public function getAbilityNameAttribute()
    {
        $abilities = [
            'breaking_ice' => '破冰力',
            'empathy' => '共情度',
            'conversation_flow' => '推进技巧',
            'general' => '通用'
        ];

        return $abilities[$this->target_ability] ?? '未知能力';
    }

    /**
     * 作用域：活跃的内容
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * 作用域：按类型筛选
     */
    public function scopeByType($query, $type)
    {
        return $query->where('content_type', $type);
    }

    /**
     * 作用域：按目标能力筛选
     */
    public function scopeByAbility($query, $ability)
    {
        return $query->where('target_ability', $ability);
    }
}